home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7326 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: cypher.3do.com!user
  2. From: tsw@3do.com (Tom Watson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: sscanf bug??????
  5. Date: Thu, 15 Feb 1996 16:01:24 -0800
  6. Organization: The 3DO Corporation
  7. Distribution: world
  8. Message-ID: <tsw-1502961601240001@cypher.3do.com>
  9. References: <4fimvo$82s@fnord.dfw.net> <4frr9j$ie8@fnord.dfw.net>
  10. NNTP-Posting-Host: cypher.3do.com
  11.  
  12. In article <4frr9j$ie8@fnord.dfw.net>, jtmcap@dfw.dfw.net (Jerry Jackson) wrote:
  13.  
  14. > Jerry Jackson (jtmcap@dfw.dfw.net) wrote:
  15. > : the following is a program compiled using microway ndp c/c++ compiler.
  16. > : 
  17. > : #include <stdio.h>
  18. > : #include <string.h>
  19. > : 
  20. > : main()
  21. > : {
  22. > :       char str_1[] = "013196";
  23. > :       char str_2[] = "13196";
  24. > :       long res_1, res_2;
  25. > : 
  26. > :       sscanf(str_1,"%d",&res_1);
  27. > :       sscanf(str_2,"%d",&res_2);
  28. > : 
  29. > :       printf("\nres_1 = %d",res_1);
  30. > :       printf("\nres_2 = %d",res_2);
  31. > : }
  32. > : 
  33. > : the output looks like this:
  34. > : 
  35. > : res_1 = 89
  36. > : res_2 = 13196
  37. > : 
  38. > : 
  39. > : microway says that the leading zero causes sscanf to do an octal 
  40. > : conversion on the integer.  i have not found any documentation to verify 
  41. > : this.  also other compilers that i use return the value 13196 for both 
  42. > : calls to sscanf.
  43. > : 
  44. > : bug or undocumented feature?
  45. > : 
  46. > additional information:  changing the format to sscanf(str_1,"%ld",&res_1)
  47. > gives the same result as sscanf(str_1,"%d",&res_1).
  48. > also this is using microway ndp-c version 4.6.
  49.  
  50. According to my sources (K&R 2, B1.3, p246, table B-2):
  51. %d scans a decimal integer.
  52. %i scans a decimal/octal/hex integer (leading non-zero, leading zero,
  53. leading 0x).
  54.  
  55. Another source (H&S 4, 15.8, p360 table 15-4):
  56. %d has input format [-|+]dd..d
  57. %i has input format [-|+][0[x]]d..d (with footnote)
  58.    footnote explains base conversion.
  59.  
  60. Still another source (_Standard C_ by Plauger & Brodie)
  61.    (I used the HTML version)
  62. Says that '%d' is the same as 'strtol' with base 10, and '%i' is the same
  63. as 'strtol' with base of zero (input defines).
  64.  
  65. All in all it looks like you have uncovered a bug in your run-time.  It
  66. shouldn't work like that!!
  67.  
  68. I'd yell REAL loud at the vendor and tell them to FIX it.
  69.  
  70. Good luck
  71.  
  72. -- 
  73. Tom Watson
  74. tsw@3do.com         (Home: tsw@johana.com)
  75.